package com.example.demo.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.MessageBody;
import com.example.demo.entity.SystemInfo;
import com.example.demo.utils.ComputerMonitorUtil;
import com.example.demo.utils.HttpClientUtil;
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created hongsou
 * author:fh
 * Date:2019/4/29
 * Time:9:49
 * desc:获取windows硬盘信息 内存信息
 **/
@RestController
@RequestMapping("/sys/")
public class SystemController {

    //    double cpuUsage = ComputerMonitorUtil.getCpuUsage();
//    //当前系统的内存使用率
//    double memUsage = ComputerMonitorUtil.getMemUsage();
//    //当前系统的硬盘使用率
//    double diskUsage = ComputerMonitorUtil.getDiskUsage();
    private static Log logger = LogFactory.getLog(SystemController.class);

    /**
     * @auther: fh
     * @date 2019/4/29 9:50
     * @des 获取系统内存信息
     */
    @RequestMapping("getMemUsage")
    public MessageBody getMemUsage() {
        return MessageBody.getMessageBody(ComputerMonitorUtil.getMemUsage());
    }

    /**
     * @auther: fh
     * @date 2019/4/29 9:51
     * @des 获取硬盘信息
     */
    @RequestMapping("getDiskUsage")
    public MessageBody getDiskUsage() {
        return MessageBody.getMessageBody(ComputerMonitorUtil.getDiskUsage());
    }

    /**
     * @auther: fh
     * @date 2019/4/29 9:57
     * @des 获取cpu使用率
     */
    @RequestMapping("getCpuUsage")
    public MessageBody getCpuUsage() {
        return MessageBody.getMessageBody(ComputerMonitorUtil.getCpuUsage());
    }

    @RequestMapping("getHttpDisk")
    public MessageBody getHttpDisk() {
        JSONObject json = (JSONObject) JSONObject.parse("");

        String s1 = HttpClientUtil.postJson("http://192.168.78.129:8011/sys/getDiskUsage", "");
        logger.info(s1);
        Gson gson = new Gson();
        return gson.fromJson(s1, MessageBody.class);
    }

    //    @RequestMapping("getHttpCpu")
//    public MessageBody getHttpCpu(){
//
//    }
//    @RequestMapping("getHttpMem")
//    public MessageBody getHttpMem(){
//
//    }
    @RequestMapping("getHttpSys")
    public SystemInfo getHttpSys() {
        JSONObject json = (JSONObject) JSONObject.parse("");
        String ip = "192.168.78.129:8011";
        String s1 = HttpClientUtil.postJson("http://" + ip +
                "/sys/getSystemInfo", "");
        logger.info(s1);
        Gson gson = new Gson();
        SystemInfo systemInfo = gson.fromJson(s1, SystemInfo.class);
        systemInfo.setIp(ip);
        return systemInfo;
    }
}
